
package controllers

import (
	"html/template"
	"net/http"
	"os"
	"go-web/db"
	"go-web/models"
	"sort"

	"github.com/astaxie/beego"
	"fmt"
)

type BigclassList []models.Bigclass

func (list BigclassList) Len() int {
	return len(list)
}

func (list BigclassList) Less(i, j int) bool {
	return list[i].Order < list[j].Order
}

func (list BigclassList) Swap(i, j int) {
	list[i], list[j] = list[j], list[i]
}

var AllBigClisses BigclassList

func (b BigclassList) GetNameById(id int) string {
	for _, v := range b {
		if v.Id == id {
			return v.Name
		}
	}
	return ""
}

func GetBigClassById(bid int) *models.Bigclass {
	if bid < 0 || bid > AllBigClisses.Len() {
		return nil
	}
	return &AllBigClisses[bid-1]
}

func GetAllBigClass() {
	fmt.Println("开始查询")
	err := db.Mysql.Asc("Order").Find(&AllBigClisses)
	if err != nil {
		os.Exit(1)
	}
	fmt.Println("查询完成")

	sort.Sort(&AllBigClisses)
	fmt.Println("查询结束")

}

func SetBigClassActive(id int) {
	for k, _ := range NavigationBar {
		if NavigationBar[k].Bc.Id == id {
			NavigationBar[k].Active = true
		} else {
			NavigationBar[k].Active = false
		}
	}
}

func GetBigClassName(id int) string {
	if id <= 0 || id > len(AllBigClisses) {
		return ""
	}
	return AllBigClisses[id-1].Name
}

type Navigation struct {
	Bc		models.Bigclass
	Scs 	SmallClassList
	Active	bool
}

var NavigationBar []Navigation

func GetFirstSmallIdByBid(bid int) int {
	if bid > len(NavigationBar) || bid < 0 {
		return -1
	}
	nav := NavigationBar[bid-1]
	if len(nav.Scs) > 0 {
		return nav.Scs[0].Id
	}
	return 0
}

func GetSmallsByBid(bid int) []models.Smallclass {
	if bid >= len(NavigationBar) || bid < 0 {
		return make([]models.Smallclass, 0)
	}
	return NavigationBar[bid-1].Scs
}

func GetNavigationBar() {
	for _, v := range AllBigClisses {
		Nav := Navigation{Bc: v}

		for _, vb := range AllSmallClassList {
			if vb.Bigclassid == v.Id {
				Nav.Scs = append(Nav.Scs, vb)
			}
		}
		sort.Sort(Nav.Scs)
		NavigationBar = append(NavigationBar, Nav)
	}
}

func init() {
	GetAllBigClass()
	GetAllSmallClass()
	GetNavigationBar()
	beego.ErrorHandler("404", notFound)
}

var errtpl = `
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>{{.Title}}</title>
		<style type="text/css">
			* {
				margin:0;
				padding:0;
			}
			body {
				background-color:#EFEFEF;
				font: .9em "Lucida Sans Unicode", "Lucida Grande", sans-serif;
			}
			#wrapper{
				width:600px;
				margin:40px auto 0;
				text-align:center;
				-moz-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
				-webkit-box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
				box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
			}
			#wrapper h1{
				color:#FFF;
				text-align:center;
				margin-bottom:20px;
			}
			#wrapper a{
				display:block;
				font-size:.9em;
				padding-top:20px;
				color:#FFF;
				text-decoration:none;
				text-align:center;
			}
			#container {
				width:600px;
				padding-bottom:15px;
				background-color:#FFFFFF;
			}
			.navtop{
				height:40px;
				background-color:#24B2EB;
				padding:13px;
			}
			.content {
				padding:10px 10px 25px;
				background: #FFFFFF;
				margin:;
				color:#333;
			}
			a.button{
				color:white;
				padding:15px 20px;
				text-shadow:1px 1px 0 #00A5FF;
				font-weight:bold;
				text-align:center;
				border:1px solid #24B2EB;
				margin:0px 200px;
				clear:both;
				background-color: #24B2EB;
				border-radius:100px;
				-moz-border-radius:100px;
				-webkit-border-radius:100px;
			}
			a.button:hover{
				text-decoration:none;
				background-color: #24B2EB;
			}
		</style>
	</head>
	<body>
		<div id="wrapper">
			<div id="container">
				<div class="navtop">
					<h1>{{.Title}}</h1>
				</div>
				<div id="content">
					{{.Content}}
					<a href="/" title="Home" class="button">返回首页</a><br />
					<br>Powered by 李维 {{.LintulVersion}}
				</div>
			</div>
		</div>
	</body>
</html>
`

func notFound(rw http.ResponseWriter, r *http.Request) {
	t, _ := template.New("beegoerrortemp").Parse(errtpl)
	data := map[string]interface{}{
		"Title":         "李维",
		"LintulVersion": "V1.0",
	}
	data["Content"] = template.HTML("<br>你访问的页面不存在." +
		"<br>你出现在这里可能因为:" +
		"<br><br><ul>" +
		"<br>浏览器输入错误的网址" +
		"<br>这个页面太长" +
		"<br>你可能迷路了" +
		"<br>你喜欢	404	页面" +
		"</ul>")
	t.Execute(rw, data)
}

type IndexController struct {
	beego.Controller
}

type IndexViewData struct {
	Url		string
	BName	string
	Data 	[]models.Detail
}

func (c *IndexController) Get() {
	Carousels := make([]models.Carousel, 0)
	db.Mysql.Desc("id").Limit(10).Find(&Carousels)
	c.Data["Lunbotu"] = Carousels
	var bt []models.Carousel
	if len(Carousels) > 4 {
		bt = Carousels[:4]
	} else {
		bt = Carousels
	}
	c.Data["XYXWLBT"] = bt
	tzgg := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 2, 5).Desc("updatetime").Limit(6, 0).Find(&tzgg)
	c.Data["TZGG"] = tzgg
	c.Data["TZGGB"] = len(tzgg) >= 6
	xyxw := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 2, 4).Desc("updatetime").Limit(6, 0).Find(&xyxw)
	c.Data["XYXW"] = xyxw
	c.Data["XYXWB"] = len(xyxw) >= 6
	bkjy := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 7, 22).Desc("updatetime").Limit(6, 0).Find(&bkjy)
	c.Data["BKJY"] = bkjy
	c.Data["BKJYB"] = len(bkjy) >= 6
	yjsjy := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 7, 23).Desc("updatetime").Limit(6, 0).Find(&yjsjy)
	c.Data["YJSJY"] = yjsjy
	c.Data["YJSJYB"] = len(yjsjy) >= 6
	xsdt := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=?", 6).Desc("updatetime").Limit(6, 0).Find(&xsdt)
	c.Data["XSDT"] = xsdt
	c.Data["XSDTB"] = len(xsdt) >= 6
	ywxx := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 2, 6).Desc("updatetime").Limit(6, 0).Find(&ywxx)
	c.Data["YWXX"] = ywxx
	c.Data["YWXXB"] = len(ywxx) >= 6
	fwzn := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 2, 7).Desc("updatetime").Limit(6, 0).Find(&fwzn)
	c.Data["FWZN"] = fwzn
	c.Data["FWZNB"] = len(fwzn) >= 6
	zlxz := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=?", 9).Desc("updatetime").Limit(6, 0).Find(&zlxz)
	c.Data["ZLXZ"] = zlxz
	c.Data["ZLXZB"] = len(zlxz) >= 6
	szdw := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 4, 12).Desc("updatetime").Limit(6, 0).Find(&szdw)
	c.Data["SZDW"] = szdw
	c.Data["SZDWB"] = len(szdw) >= 6
	zyjs := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 4, 13).Desc("updatetime").Limit(6, 0).Find(&zyjs)
	c.Data["ZYJS"] = zyjs
	c.Data["ZYJSB"] = len(zyjs) >= 6
	jpkc := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 4, 14).Desc("updatetime").Limit(6, 0).Find(&jpkc)
	c.Data["JPKC"] = jpkc
	c.Data["JPKCB"] = len(jpkc) >= 6
	xsxx := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 4, 15).Desc("updatetime").Limit(6, 0).Find(&xsxx)
	c.Data["XSXX"] = xsxx
	c.Data["XSXXB"] = len(xsxx) >= 6
	xsjl := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 4, 16).Desc("updatetime").Limit(6, 0).Find(&xsjl)
	c.Data["XSJL"] = xsjl
	c.Data["XSJLB"] = len(xsjl) >= 6

	xsgz1 := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 5, 17).Desc("updatetime").Limit(6, 0).Find(&xsgz1)
	c.Data["XSGZ1"] = xsgz1
	c.Data["XSGZ1B"] = len(xsgz1) >= 6
	xsgz2 := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 5, 18).Desc("updatetime").Limit(6, 0).Find(&xsgz2)
	c.Data["XSGZ2"] = xsgz2
	c.Data["XSGZ2B"] = len(xsgz2) >= 6
	xsgz3 := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 5, 19).Desc("updatetime").Limit(6, 0).Find(&xsgz3)
	c.Data["XSGZ3"] = xsgz3
	c.Data["XSGZ3B"] = len(xsgz3) >= 6
	xsgz4 := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 5, 20).Desc("updatetime").Limit(6, 0).Find(&xsgz4)
	c.Data["XSGZ4"] = xsgz4
	c.Data["XSGZ4B"] = len(xsgz4) >= 6
	xsgz5 := make([]models.Detail, 0)
	db.Mysql.Where("Bigclassid=? and Smallclassid=?", 5, 21).Desc("updatetime").Limit(6, 0).Find(&xsgz5)
	c.Data["XSGZ5"] = xsgz5
	c.Data["XSGZ5B"] = len(xsgz5) >= 6
	c.Data["Navigation"] = NavigationBar
	c.TplName = "index.html"
}